/**
 * 报表设计公司(2011)
 */
package com.reportgear.swing.util;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;

import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 * 标准IO消息重定向
 * 
 * @version 1.0 2011-3-14
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ANNCSR 2.0
 * 
 */
public final class GuiPrintStream extends PrintStream {
	// 单列对象
	private static GuiPrintStream	INSTANCE	= new GuiPrintStream(new ByteArrayOutputStream());

	/**
	 * 获取单例对象
	 * 
	 * @return 单例对象
	 */
	public static GuiPrintStream getInstance() {
		return GuiPrintStream.INSTANCE;
	}

	// 滚动消息面板
	private final JScrollPane	scrollPane;

	// IO数据目标组件
	private final JTextArea		outArea;

	/**
	 * 构造函数
	 * 
	 * @param out
	 */
	private GuiPrintStream(final OutputStream out) {
		super(out);

		this.outArea = new JTextArea();
		this.scrollPane = new JScrollPane(this.outArea);
	}

	/**
	 * 获取输出面板,可以置入其它显示组件之中
	 * 
	 * @return 输出面板
	 */
	public JComponent getOutPane() {
		return this.scrollPane;
	}

	@Override
	public void write(final byte[] buf, final int off, final int len) {
		this.outArea.append(new String(buf, off, len));
	}
}
